//
// Created by v on 19-7-8.
//

#ifndef CUTEDEVICE_C600_CMCONFIGSIMPLEJSON_H
#define CUTEDEVICE_C600_CMCONFIGSIMPLEJSON_H

#include "rapidjson/document.h"
#include "rapidjson/filewritestream.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/writer.h"

class CMConfigSimpleJson
{
  public:
    CMConfigSimpleJson(const char *filename);
    CMConfigSimpleJson(const std::string &buffer);
    CMConfigSimpleJson(const rapidjson::Value &root);
    CMConfigSimpleJson();

    int open(const std::string &buffer);
    int open(const char *filename);

    int writeBack(const char *filename = NULL);
    int generateString(std::string &result);

    int getGeneralString(const char *key, std::string &result);
    int getGeneralInt(const char *key);
    int getGeneralBool(const char *key, bool &result);

    int  setGeneralString(rapidjson::Value &obj, const char *key,
                          const char *value);
    int  setGeneralInt(rapidjson::Value &obj, const char *key, int value);
    int  setGeneralInt64(rapidjson::Value &obj, const char *key, int64_t value);
    int  setGeneralBool(rapidjson::Value &obj, const char *key, bool value);
    bool getValid() { return m_valid; }

  protected:
    rapidjson::Document &getRoot() { return m_root; }

  private:
    rapidjson::Document m_root;
    std::string         m_filename;
    bool                m_valid;
};

#endif // CUTEDEVICE_C600_CMCONFIGSIMPLEJSON_H
